﻿// <copyright file="ValidateNetworkButton.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>ValidateNetworkButton class</summary>

namespace BESASM.EMGAATS.UI.ArcMap
{
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Text;
  using System.Windows.Forms;
  using BESASM.EMGAATS.BusinessClasses;
  using ESRI.ArcGIS.Carto;
  using ESRI.ArcGIS.esriSystem;
  using ESRI.ArcGIS.Geodatabase;
  using ESRI.ArcGIS.Geometry;

  /// <summary>
  /// A button to validate the loaded network
  /// </summary>
  public class ValidateNetworkButton : ESRI.ArcGIS.Desktop.AddIns.Button
  {
    /// <summary>
    /// Called by ArcMap when the user clicks the button.
    /// </summary>
    protected override void OnClick()
    {
      FormsUtility.SafeExecute(delegate
      {
        NetworkValidator validator = new NetworkValidator(RuleSet.MasterDataCommit);

        System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;
        try
        {
          var validation = validator.Validate(EmgaatsExtension.Instance.NetworkManager.Network);
          if (!validation.IsValid)
          {
            FormsUtility.ShowValidation(validation);
          }
          else
          {
            FormsUtility.ShowMessage("The network is valid!");
          }
        }
        finally
        {
          System.Windows.Forms.Cursor.Current = Cursors.Default;
        }
      });
    }

    /// <summary>
    /// Called by ArcMap when this button is to be updated.
    /// </summary>
    protected override void OnUpdate()
    {
      if (EmgaatsExtension.Instance != null && EmgaatsExtension.Instance.NetworkManager != null)
      {
        this.Enabled = EmgaatsExtension.Instance.NetworkManager.IsLoaded;
      }
      else
      {
        this.Enabled = false;
      }
    }
  }
}
